
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <unordered_set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int N = 4020;
int num[N];
void solve()
{
	int n;
	cin >> n;
	vector<pii> p(n);
	int cnt = 0;
	for (int i = 0; i < n; i++)
	{
		scanf("%d %d", &p[i].first, &p[i].second);
		num[++cnt] = p[i].first;
		num[++cnt] = p[i].second;
	}
	sort(p.begin(), p.end());
	sort(num +1, num + cnt+1);
	cnt = unique(num+1, num + cnt+1) - num - 1;
	auto get = [&](int x) {
		return lower_bound(num +1, num + cnt+1, x) - num;
	};
	for (auto& [x, y] : p) {
		x = get(x);
		y = get(y);
	}
	vector<vector<int>> pos(cnt+1);
	for (int i = 0; i < n; i++)
	{
		auto [l1, r1] = p[i];
		for (int j = i+1; j < n; j++)
		{
			auto [l2, r2] = p[j];
			if (l2 <= r1)
			{
				pos[max(r1, r2)].push_back(min(l1, l2));
			}
			else break;
		}
	}
	vector<int> dp(cnt + 1);
	for (int i = 1; i <= cnt; i++)
	{
		int res = 0;
		for (auto l : pos[i])
		{
			res = max(res, dp[l - 1] + 1);
		}
		dp[i] = max(dp[i], res);
		for (int j = 1; j <= i; j++)
		{
			dp[j] = max(dp[j], dp[j - 1]);
		}
	}
	cout << n - dp[cnt] * 2 << endl;
}
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		solve();
	}

	return 0;
}

